package Sistema.professor.aluno;

/**
 * A classe Cadastro implementa um cadastro de professores.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

public class Cadastro implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 97717595048394156L;

	@SuppressWarnings("rawtypes")
	private ArrayList array;

	public Cadastro(int capacidade) {
		try {
			setArray(new ArrayList<>(capacidade));
		} catch (IllegalArgumentException e) {
			System.out.println("Número negativo: Valor Invalido!");
		}
	}

	@SuppressWarnings("unchecked")
	public void insere(Professor p) {
		this.array.add(p);
	}

	@SuppressWarnings("unchecked")
	public boolean insere(Professor p, int posicao) throws Exception {
		try {
			this.array.add(posicao, p);
		} catch (IndexOutOfBoundsException e) {
			System.out.println(e);
			return false;
		}
		return true;
	}

	@SuppressWarnings("rawtypes")
	public boolean remove(String nome) {
		try {
			Iterator i = array.iterator();
			while (i.hasNext()) {
				Professor p = (Professor) i.next();
				if (p.getNome().equalsIgnoreCase(nome)) {
					i.remove();
					return true;
				}
			}
		} catch (UnsupportedOperationException e) {
			System.out.println(e);
			return false;
		} catch (Exception e) {
			System.out.println(e);
			return false;
		}
		return false;
	}

	// Não e possivel retorna false!
	@SuppressWarnings("rawtypes")
	public Professor consulta(String nome) {
		try {
			Iterator i = array.iterator();
			while (i.hasNext()) {
				Professor p = (Professor) i.next();
				if (p.getNome().equalsIgnoreCase(nome))
					return p;
			}
		} catch (Exception e) {
			System.out.println(e);
			return null;
		}
		return null;
	}

	public Professor get(int posicao) {
		try {
			return (Professor) array.get(posicao);
		} catch (IndexOutOfBoundsException e) {
			System.out.println(e);
			return null;
		}
	}

	public void guardarEmArquivo() {
		if (!array.isEmpty()) {
			ObjectOutputStream obj;
			FileOutputStream out;
			try {
				out = new FileOutputStream("Cadastro.txt");
				obj = new ObjectOutputStream(out);

				for(Object o : array){//TODO
					obj.writeObject((Professor)o);
				}

				obj.close();

			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		} else {
			System.out.println("Nenhum professor cadastrado");
		}
	}

	public void recuperarEmArquivo() {
		if (!array.isEmpty()) {
			ObjectInputStream obj;
			FileInputStream in;
			try {
				in = new FileInputStream("Cadastro.txt");
				obj = new ObjectInputStream(in);

				boolean aux = false;
				while (!aux) {
					Professor line;
					line = (Professor) obj.readObject();
					if (line == null)
						aux = true;
					else
						System.out.println(line);
				}

				obj.close();

			}catch(EOFException e){
				System.out.println("Fim");
			}
			catch (FileNotFoundException ex) {
				ex.printStackTrace();
			} catch (IOException ex) {
				ex.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("Nenhum professor cadastrado");
		}
	}

	@SuppressWarnings("rawtypes")
	public ArrayList getArray() {
		return array;
	}

	@SuppressWarnings("rawtypes")
	public void setArray(ArrayList array) {
		this.array = array;
	}
}
